home *** CD-ROM | disk | FTP | other *** search
/ PC Open 101 / PC Open 101 CD 1.bin / CD1 / INTERNET / EMAIL / pop file / setup.exe / Classifier / popfile.sql < prev    next >
Encoding:
Text File  |  2004-09-22  |  21.4 KB  |  448 lines

  1. -- POPFILE SCHEMA 3
  2. -- ---------------------------------------------------------------------------------------------
  3. --
  4. -- popfile.schema - POPFile's database schema
  5. --
  6. -- Copyright (c) 2003-2004 John Graham-Cumming
  7. --
  8. --   This file is part of POPFile
  9. --
  10. --   POPFile is free software; you can redistribute it and/or modify
  11. --   it under the terms of the GNU General Public License as published by
  12. --   the Free Software Foundation; either version 2 of the License, or
  13. --   (at your option) any later version.
  14. --
  15. --   POPFile is distributed in the hope that it will be useful,
  16. --   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. --   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. --   GNU General Public License for more details.
  19. --
  20. --   You should have received a copy of the GNU General Public License
  21. --   along with POPFile; if not, write to the Free Software
  22. --   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. --
  24. -- ---------------------------------------------------------------------------------------------
  25.  
  26. -- An ASCII ERD (you might like to find the 'users' table first and work from there)
  27. --
  28. --      +---------------+         +-----------------+
  29. --      | user_template |         | bucket_template |
  30. --      +---------------+         +-----------------+
  31. --      |      id       |-----+   |       id        |---+
  32. --      |     name      |     |   |      name       |   |
  33. --      |     def       |     |   |       def       |   |
  34. --      +---------------+     |   +-----------------+   |
  35. --                            |                         |
  36. --      +---------------+     |     +---------------+   |
  37. --      |  user_params  |     |     | bucket_params |   |
  38. --      +---------------+     |     +---------------+   |
  39. --      |      id       |     |     |      id       |   |
  40. --  +---|    userid     |     | +---|   bucketid    |   |
  41. --  |   |     utid      |-----+ |   |     btid      |---+
  42. --  |   |     val       |       |   |     val       |
  43. --  |   +---------------+       |   +---------------+
  44. --  |                           |                      +----------+
  45. --  |                           |                      |  matrix  |      +-------+
  46. --  |                           |   +---------+        +----------+      | words |
  47. --  |      +----------+         |   | buckets |        |    id    |      +-------+
  48. --  |      |   users  |         |   +---------+        |  wordid  |------|  id   |
  49. --  |      +----------+      /--+---|    id   |=====---| bucketid |      |  word |
  50. --  +----==|    id    |-----(-------| userid  |     \  |  times   |      +-------+
  51. --      /  |   name   |     |       |  name   |     |  | lastseen |
  52. --      |  | password |     |       | pseudo  |     |  +----------+
  53. --      |  +----------+     |       +---------+     |
  54. --      |                   |                       |
  55. --      |                   |        +-----------+  |
  56. --      |                   |        |  magnets  |  |
  57. --      |   +------------+  |        +-----------+  |     +--------------+
  58. --      |   |   history  |  |     +--|    id     |  |     | magnet_types |
  59. --      |   +------------+  |     |  | bucketid  |--+     +--------------+
  60. --      |   |     id     |  |     |  |   mtid    |--------|      id      |
  61. --      +---|   userid   |  |     |  |   val     |        |     mtype    |
  62. --          |   hdr_from |  |     |  |   seq     |        |    header    |
  63. --          |   hdr_to   |  |     |  +-----------+        +--------------+
  64. --          |   hdr_cc   |  |     |
  65. --          | hdr_subject|  |     |
  66. --          |  bucketid  |--+     |
  67. --          |  usedtobe  |--/     |
  68. --          |  magnetid  |--------+
  69. --          |  hdr_date  |
  70. --          | inserted   |
  71. --          |    hash    |
  72. --          | committed  |
  73. --          |    size    |
  74. --          +------------+
  75. --
  76.  
  77. -- TABLE DEFINITIONS
  78.  
  79. -- ---------------------------------------------------------------------------------------------
  80. --
  81. -- popfile - data about the database
  82. --
  83. -- ---------------------------------------------------------------------------------------------
  84.  
  85. create table popfile ( id integer primary key,
  86.                        version integer         -- version number of this schema
  87.                      );
  88.  
  89. -- ---------------------------------------------------------------------------------------------
  90. --
  91. -- users - the table that stores the names and password of POPFile users
  92. --
  93. -- v0.21.0: With this release POPFile does not have an internal concept of
  94. -- 'user' and hence this table consists of a single user called 'admin', once
  95. -- we do the full multi-user release of POPFile this table will be used and
  96. -- there will be suitable APIs and UI to modify it
  97. --
  98. -- ---------------------------------------------------------------------------------------------
  99.  
  100. create table users ( id integer primary key,  -- unique ID for this user
  101.                      name varchar(255),       -- textual name of the user
  102.                      password varchar(255),   -- user's password
  103.                      unique (name)            -- the user name must be unique
  104.                    );
  105.  
  106. -- ---------------------------------------------------------------------------------------------
  107. --
  108. -- buckets - the table that stores the name of POPFile buckets and relates
  109. --           them to users. 
  110. --
  111. -- Note: A single user may have multiple buckets, but a single bucket only has
  112. -- one user.  Hence there is a many-to-one relationship from buckets to users.
  113. --
  114. -- ---------------------------------------------------------------------------------------------
  115.  
  116. create table buckets( id integer primary key, -- unique ID for this bucket
  117.                       userid integer,         -- corresponds to an entry in
  118.                                               -- the users table
  119.                       name varchar(255),      -- the name of the bucket
  120.                       pseudo int,             -- 1 if this is a pseudobucket
  121.                                               -- (i.e. one POPFile uses internally)
  122.                       unique (userid,name)    -- a user can't have two buckets
  123.                                               -- with the same name
  124.                     );
  125.  
  126. -- ---------------------------------------------------------------------------------------------
  127. --
  128. -- words - the table that creates a unique ID for a word.  
  129. --
  130. -- Words and buckets come together in the matrix table to form the corpus of words for
  131. -- each user.
  132. --
  133. -- ---------------------------------------------------------------------------------------------
  134.  
  135. create table words(   id integer primary key, -- unique ID for this word
  136.                       word varchar(255),      -- the word
  137.                       unique (word)           -- each word is unique
  138.                   );
  139.  
  140. -- ---------------------------------------------------------------------------------------------
  141. --
  142. -- matrix - the corpus that consists of buckets filled with words.  Each word
  143. --          in each bucket has a word count.
  144. --
  145. -- ---------------------------------------------------------------------------------------------
  146.  
  147. create table matrix( id integer primary key,   -- unique ID for this entry
  148.                      wordid integer,           -- an ID in the words table
  149.                      bucketid integer,         -- an ID in the buckets table
  150.                      times integer,            -- number of times the word has
  151.                                                -- been seen
  152.                      lastseen date,            -- last time the record was read
  153.                                                -- or written
  154.                      unique (wordid, bucketid) -- each word appears once in a bucket 
  155.                    );
  156.  
  157. -- ---------------------------------------------------------------------------------------------
  158. --
  159. -- user_template - the table of possible parameters that a user can have.  
  160. --
  161. -- For example in the users table there is just an password associated with
  162. -- the user.  This table provides a flexible way of creating per user
  163. -- parameters. It stores the definition of the parameters and the the
  164. -- user_params table relates an actual user with each parameter
  165. --
  166. -- ---------------------------------------------------------------------------------------------
  167.  
  168. create table user_template( id integer primary key,  -- unique ID for this entry
  169.                           name varchar(255),         -- the name of the
  170.                                                      -- parameter
  171.                           def varchar(255),          -- the default value for
  172.                                                      -- the parameter
  173.                           unique (name)              -- parameter name's are unique 
  174.                         );
  175.  
  176. -- ---------------------------------------------------------------------------------------------
  177. --
  178. -- user_params - the table that relates users with user parameters (as defined
  179. --               in user_template) and specific values.
  180. --
  181. -- ---------------------------------------------------------------------------------------------
  182.  
  183. create table user_params( id integer primary key,    -- unique ID for this
  184.                                                      -- entry
  185.                           userid integer,            -- a user
  186.                           utid integer,              -- points to an entry in 
  187.                                                      -- user_template
  188.                           val varchar(255),          -- value for the
  189.                                          -- parameter
  190.                           unique (userid, utid)      -- each user has just one
  191.                                          -- instance of each parameter
  192.                         );
  193.  
  194. -- ---------------------------------------------------------------------------------------------
  195. --
  196. -- bucket_template - the table of possible parameters that a bucket can have.  
  197. --
  198. -- See commentary for user_template for an explanation of the philosophy
  199. --
  200. -- ---------------------------------------------------------------------------------------------
  201.  
  202. create table bucket_template( id integer primary key,  -- unique ID for this entry
  203.                               name varchar(255),       -- the name of the
  204.                                                        -- parameter
  205.                               def varchar(255),        -- the default value for
  206.                                                        -- the parameter
  207.                               unique (name)            -- parameter name's are unique 
  208.                             );
  209.  
  210. -- ---------------------------------------------------------------------------------------------
  211. --
  212. -- bucket_params - the table that relates buckets with bucket parameters (as defined
  213. --                 in bucket_template) and specific values.
  214. --
  215. -- ---------------------------------------------------------------------------------------------
  216.  
  217. create table bucket_params( id integer primary key,    -- unique ID for this
  218.                                                        -- entry
  219.                             bucketid integer,          -- a bucket
  220.                             btid integer,              -- points to an entry in 
  221.                                                        -- bucket_template
  222.                             val varchar(255),          -- value for the
  223.                                            -- parameter
  224.                             unique (bucketid, btid)    -- each bucket has just one
  225.                                            -- instance of each parameter
  226.                         );
  227.  
  228. -- ---------------------------------------------------------------------------------------------
  229. --
  230. -- magnet_types - the types of possible magnet and their associated header
  231. --
  232. -- ---------------------------------------------------------------------------------------------
  233.  
  234. create table magnet_types( id integer primary key,  -- unique ID for this entry
  235.                            mtype varchar(255),      -- the type of magnet
  236.                                                     -- (e.g. from)
  237.                            header varchar(255),     -- the header (e.g. From)
  238.                            unique (mtype)           -- types are unique
  239.                          );
  240.  
  241. -- ---------------------------------------------------------------------------------------------
  242. --
  243. -- magnets - relates specific buckets to specific magnet types with actual
  244. -- magnet values
  245. --
  246. -- ---------------------------------------------------------------------------------------------
  247.  
  248. create table magnets( id integer primary key,    -- unique ID for this entry
  249.                       bucketid integer,          -- a bucket
  250.                       mtid integer,              -- the magnet type
  251.                       val varchar(255),          -- value for the magnet
  252.                       comment varchar(255),      -- user defined comment
  253.                       seq integer                -- used to set the order of magnets
  254.                     );
  255.  
  256. -- ---------------------------------------------------------------------------------------------
  257. --
  258. -- history - this table contains the items in the POPFile history that
  259. -- are managed by POPFile::History
  260. --
  261. -- ---------------------------------------------------------------------------------------------
  262.  
  263. create table history( id integer primary key,    -- unique ID for this entry
  264.                       userid integer,            -- which user owns this
  265.                       committed integer,         -- 1 if this item has been committed
  266.                       hdr_from    varchar(255),  -- The From: header 
  267.                       hdr_to      varchar(255),  -- The To: header            
  268.                       hdr_cc      varchar(255),  -- The Cc: header            
  269.                       hdr_subject varchar(255),  -- The Subject: header
  270.                       hdr_date    date,          -- The Date: header
  271.                       hash        varchar(255),  -- MD5 message hash
  272.                       inserted    date,          -- When this was added
  273.                       bucketid integer,          -- Current classification
  274.                       usedtobe integer,          -- Previous classification
  275.                       magnetid integer,          -- If classified with magnet
  276.                       sort_from   varchar(255),  -- The From: header 
  277.                       sort_to     varchar(255),  -- The To: header            
  278.                       sort_cc     varchar(255),  -- The Cc: header            
  279.                       size        integer        -- Size of the message in Bytes
  280.                     );
  281.  
  282. -- MySQL SPECIFIC 
  283.  
  284. -- ---------------------------------------------------------------------------------------------
  285. --
  286. -- NOTE: The following alter table statements are required by MySQL in order
  287. --       to get the ID fields to auto_increment on inserts.
  288. --
  289. -- ---------------------------------------------------------------------------------------------
  290.  
  291.  
  292. alter table buckets modify id int(11) auto_increment;
  293. alter table bucket_params modify id int(11) auto_increment;
  294. alter table bucket_template modify id int(11) auto_increment;
  295. alter table magnets modify id int(11) auto_increment;
  296. alter table magnet_types modify id int(11) auto_increment;
  297. alter table matrix modify id int(11) auto_increment;
  298. alter table user_params modify id int(11) auto_increment;
  299. alter table user_template modify id int(11) auto_increment;
  300. alter table users modify id int(11) auto_increment;
  301. alter table words modify id int(11) auto_increment;
  302. alter table history modify id int(11) auto_increment;
  303. alter table popfile modify id int(11) auto_increment;
  304.  
  305. -- MySQL treats char fields as case insensitive for searches, in order to have
  306. -- the same behavior as SQLite (case sensitive searches) we alter the word.word
  307. -- field to binary, that will trick MySQL into treating it the way we want.
  308.  
  309. alter table words modify word binary(255);
  310.  
  311.  
  312. -- MySQL enforces types, SQLite uses the concept of manifest typing, where 
  313. -- the type of a value is associated with the value itself, not the column that 
  314. -- it is stored in. POPFile has two date fields in history where POPFile
  315. -- is actually storing the unix time not a date. MySQL interprets the
  316. -- unix time as a date of 0000-00-00, whereas SQLite simply stores the
  317. -- unix time integer. The follow alter table statements redefine those
  318. -- date fields as integer for MySQL so the correct behavior is obtained
  319. -- for POPFile's use of the fields.
  320.  
  321. alter table history modify hdr_date int(11);
  322. alter table history modify inserted int(11);
  323.  
  324. -- TRIGGERS
  325.  
  326. -- ---------------------------------------------------------------------------------------------
  327. --
  328. -- delete_bucket - if a/some bucket(s) are delete then this trigger ensures
  329. --                 that entries the hang off the bucket table are also deleted
  330. --
  331. -- It deletes the related entries in the 'matrix', 'bucket_params' and
  332. -- 'magnets' tables.  
  333. --
  334. -- ---------------------------------------------------------------------------------------------
  335.  
  336. create trigger delete_bucket delete on buckets
  337.              begin
  338.                  delete from matrix where bucketid = old.id;
  339.                  delete from history where bucketid = old.id;
  340.                  delete from magnets where bucketid = old.id;
  341.                  delete from bucket_params where bucketid = old.id;
  342.              end;
  343.  
  344. -- ---------------------------------------------------------------------------------------------
  345. --
  346. -- delete_user - deletes entries that are related to a user
  347. --
  348. -- It deletes the related entries in the 'matrix' and 'user_params'.
  349. --
  350. -- ---------------------------------------------------------------------------------------------
  351.  
  352. create trigger delete_user delete on users
  353.              begin
  354.                  delete from history where userid = old.id;
  355.                  delete from buckets where userid = old.id;
  356.                  delete from user_params where userid = old.id;
  357.              end;
  358.  
  359. -- ---------------------------------------------------------------------------------------------
  360. --
  361. -- delete_magnet_type - handles the removal of a magnet type (this should be a
  362. --                      very rare thing)
  363. --
  364. -- ---------------------------------------------------------------------------------------------
  365.  
  366. create trigger delete_magnet_type delete on magnet_types
  367.              begin
  368.                  delete from magnets where mtid = old.id;
  369.              end;
  370.  
  371. -- ---------------------------------------------------------------------------------------------
  372. --
  373. -- delete_user_template - handles the removal of a type of user parameters
  374. --
  375. -- ---------------------------------------------------------------------------------------------
  376.  
  377. create trigger delete_user_template delete on user_template
  378.              begin
  379.                  delete from user_params where utid = old.id;
  380.              end;
  381.  
  382. -- ---------------------------------------------------------------------------------------------
  383. --
  384. -- delete_bucket_template - handles the removal of a type of bucket parameters
  385. --
  386. -- ---------------------------------------------------------------------------------------------
  387.  
  388. create trigger delete_bucket_template delete on bucket_template
  389.              begin
  390.                  delete from bucket_params where btid = old.id;
  391.              end;
  392.  
  393. -- Default data
  394.  
  395. -- This is schema version 3
  396.  
  397. insert into popfile ( version ) values ( 3 );
  398.  
  399. -- There's always a user called 'admin'
  400.  
  401. insert into users ( name, password ) values ( 'admin', 'e11f180f4a31d8caface8e62994abfaf' );
  402.  
  403. insert into magnets ( id, bucketid, mtid, val, comment, seq ) values ( 0, 0, 0, '', '', 0 );
  404.  
  405. -- These are the possible parameters for a bucket
  406. --
  407. -- subject      1 if should do subject modification for message classified to this bucket
  408. -- xtc          1 if should add X-Text-Classification header
  409. -- xpl          1 if should add X-POPFile-Link header
  410. -- fncount      Number of messages that were incorrectly classified, and meant to go into
  411. --                  this bucket but did not
  412. -- fpcount      Number of messages that were incorrectly classified into this bucket
  413. -- quarantine   1 if should quaratine (i.e. RFC822 wrap) messages in this bucket
  414. -- count        Total number of messages classified into this bucket
  415. -- color        The color used for this bucket in the UI
  416.  
  417. insert into bucket_template ( name, def ) values ( 'subject',    '1' ); 
  418. insert into bucket_template ( name, def ) values ( 'xtc',        '1' );
  419. insert into bucket_template ( name, def ) values ( 'xpl',        '1' );
  420. insert into bucket_template ( name, def ) values ( 'fncount',    '0' );
  421. insert into bucket_template ( name, def ) values ( 'fpcount',    '0' );
  422. insert into bucket_template ( name, def ) values ( 'quarantine', '0' );
  423. insert into bucket_template ( name, def ) values ( 'count',      '0' );
  424. insert into bucket_template ( name, def ) values ( 'color',      'black' );
  425.  
  426. -- The possible magnet types
  427.  
  428. insert into magnet_types ( mtype, header ) values ( 'from',    'From'    );
  429. insert into magnet_types ( mtype, header ) values ( 'to',      'To'      );
  430. insert into magnet_types ( mtype, header ) values ( 'subject', 'Subject' );
  431. insert into magnet_types ( mtype, header ) values ( 'cc',      'Cc'      );
  432.  
  433. -- There's always a bucket called 'unclassified' which is where POPFile puts
  434. -- messages that it isn't sure about.
  435.  
  436. insert into buckets ( name, pseudo, userid ) values ( 'unclassified', 1, 1 );
  437.  
  438. -- MySQL insists that auto_increment fields start at 1. POPFile requires
  439. -- a special magnet record with an id of 0 in order to work properly.
  440. -- The following SQL statement will fix the inserted special record
  441. -- on MySQL installs so the id is 0, the statement should do nothing
  442. -- on SQLite installs since it will not satisfy the where clause.
  443.  
  444. update magnets set id = 0 where id = 1 and (bucketid = 0 and mtid = 0);
  445.  
  446. -- END
  447.  
  448.